package com.example.demo.controller;

import com.example.demo.pojo.Order;
import com.example.demo.pojo.Response;
import com.example.demo.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/order")
@Tag(name = "订单管理", description = "订单管理相关接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Operation(summary = "加入购物车")
    @PostMapping("/add/order")
    @ApiResponse(responseCode = "200", description = "订单添加成功", content = @Content(mediaType = "application/json"))
    public ResponseEntity<Response<Order>> addOrder(@Parameter(description = "要添加的订单") @RequestBody Order order) {
        Response<Order> response = new Response<>();
        try {
            Order oldorder = orderService.findByDoubleId(order.getProduct_id(), order.getUser_id());
            Order neworder = null;
            if (oldorder != null) {
                updateOrderCount(oldorder.getProduct_id(), oldorder.getUser_id(), oldorder.getProduct_number() + order.getProduct_number());
            } else {
                neworder = orderService.addOrder(order);
            }

            response.setCode(200);
            response.setMessage("添加成功");
            response.setOrder(neworder);
            return ResponseEntity.ok(response);

        } catch (Exception e) {
            response.setCode(500);
            response.setMessage("服务器错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @Operation(summary = "购买商品")
    @PostMapping("/add/shop")//购买商品
    @ApiResponse(responseCode = "200", description = "订单添加成功", content = @Content(mediaType = "application/json"))
    public ResponseEntity<Response<Order>> addShop(@Parameter(description = "要添加的订单") @RequestBody Order order) {
        Response<Order> response = new Response<>();
        try {
            orderService.findByDoubleId(order.getProduct_id(), order.getUser_id());


            Order neworder = orderService.addOrder1(order);


            response.setCode(200);
            response.setMessage("添加成功");
            response.setOrder(neworder);
            return ResponseEntity.ok(response);


        } catch (Exception e) {
            response.setCode(500);
            response.setMessage("服务器错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @Operation(summary = "删除购物车商品")
    @DeleteMapping("delete/double_id")
    @ApiResponse(responseCode = "200", description = "订单删除成功", content = @Content(mediaType = "application/json"))
    public ResponseEntity<Response<Order>> deleteOrder(@Parameter(description = "商品ID") @RequestParam("product_id") Integer product_id,
                                                       @Parameter(description = "用户ID") @RequestParam("user_id") Integer user_id) {
        Response<Order> response = new Response<>();
        try {
            orderService.deleteOrderByDoubleId(product_id, user_id);
            response.setCode(200);
            response.setMessage("订单删除成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.setCode(500);
            response.setMessage("服务器错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @Operation(summary = "更新订单中商品数量")
    @PutMapping("/update/product_id")
    @ApiResponse(responseCode = "200", description = "订单数量更新成功", content = @Content(mediaType = "application/json"))
    public ResponseEntity<Response<Order>> updateOrderCount(@Parameter(description = "商品ID") @RequestParam("product_id") Integer product_id,
                                                            @Parameter(description = "用户ID") @RequestParam("user_id") Integer user_id,
                                                            @Parameter(description = "商品数量") @RequestParam("product_number") Integer product_number) {
        Response<Order> response = new Response<>();
        try {
            Order order = orderService.updateOrderCount(product_id, user_id, product_number);
            response.setCode(200);
            response.setMessage("订单数量更新成功");
            return ResponseEntity.ok(response);

        } catch (Exception e) {

            response.setCode(500);
            response.setMessage("服务器错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @Operation(summary = "购买购物车中商品,更新订单状态")
    @PutMapping("BuyOrder")
    @ApiResponse(responseCode = "200", description = "订单状态更新成功", content = @Content(mediaType = "application/json"))
    public ResponseEntity<Response<Order>> BuyOrder(@Parameter(description = "商品ID") @RequestParam("product_id") Integer product_id,
                                                    @Parameter(description = "用户ID") @RequestParam("user_id") Integer user_id) {
        Response<Order> response = new Response<>();
        try {
            Order order = orderService.updateOrderState(product_id, user_id);
            response.setCode(200);
            response.setMessage("订单状态更新成功");
            response.setOrder(order);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.setCode(500);
            response.setMessage("服务器错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @Operation(summary = "根据状态和user_id查询")
    @GetMapping("/find-orderstate-userid")
    @ApiResponse(responseCode = "200", description = "订单查询成功", content = @Content(mediaType = "application/json"))
    public ResponseEntity<Response<Order>> findByOrderStateAndUserId(@Parameter(description = "订单状态") @RequestParam("order_state") String order_state,
                                                                     @Parameter(description = "用户ID") @RequestParam("user_id") Integer user_id) {
        Response<Order> response = new Response<>();
        try {
            List<Order> orderList = orderService.findByOrderStateAndUserId(order_state, user_id);
            response.setCode(200);
            response.setMessage("订单查询成功");
            response.setList(orderList);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.setCode(500);
            response.setMessage("服务器错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @Operation(summary = "根据订单id查询订单")
    @GetMapping("/find-byorderid")
    @ApiResponse(responseCode = "200", description = "订单查询成功", content = @Content(mediaType = "application/json"))
    public ResponseEntity<Response<Order>> findById(@Parameter(description = "订单ID") @RequestParam("order_id") Integer order_id) {
        Response<Order> response = new Response<>();
        try {
            Order order = orderService.findById(order_id);
            response.setCode(200);
            response.setMessage("订单查询成功");
            response.setOrder(order);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.setCode(500);
            response.setMessage("服务器错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @Operation(summary = "根据订单状态，查找商品")
    @GetMapping("/find/order_state")
    @ApiResponse(responseCode = "200", description = "订单查询成功", content = @Content(mediaType = "application/json"))
    public ResponseEntity<Response<Order>> findByProductState(@Parameter(description = "订单状态") @RequestParam("order_state") String order_state) {
        Response<Order> response = new Response<>();
        try {
            response.setCode(200);
            response.setMessage("订单查询成功");
            response.setList(orderService.findByOrderState(order_state));
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.setCode(500);
            response.setMessage("服务器错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @Operation(summary = "根据用户id查询订单")
    @GetMapping("/find-byuserid")
    @ApiResponse(responseCode = "200", description = "订单查询成功", content = @Content(mediaType = "application/json"))
    public ResponseEntity<Response<Order>> findByUserId(@Parameter(description = "用户ID") @RequestParam("user_id") Integer user_id) {
        Response<Order> response = new Response<>();
        try {
            response.setCode(200);
            response.setMessage("订单查询成功");
            response.setList(orderService.findByUserId(user_id));
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.setCode(500);
            response.setMessage("服务器错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @Operation(summary = "查找所有订单")
    @GetMapping("/find-all")
    @ApiResponse(responseCode = "200", description = "订单查询成功", content = @Content(mediaType = "application/json"))
    public ResponseEntity<Response<Order>> findAll() {
        Response<Order> response = new Response<>();
        try {
            response.setCode(200);
            response.setMessage("订单查询成功");
            response.setList(orderService.findAll());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.setCode(500);
            response.setMessage("服务器错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @Operation(summary = "管理员查找订单")
    @GetMapping("/find/manager")
    @ApiResponse(responseCode = "200", description = "订单查询成功", content = @Content(mediaType = "application/json"))
    public ResponseEntity<Response<Order>> findByIDorName(@Parameter(description = "查找关键词") String keyword) {
        Response<Order> response = new Response<>();
        try {
            response.setCode(200);
            response.setMessage("订单查询成功");
            response.setList(orderService.findByIDorName(keyword));
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.setCode(500);
            response.setMessage("服务器错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @Operation(summary = "取消订单")
    @DeleteMapping("/delete/order_id")
    @ApiResponse(responseCode = "200", description = "订单取消成功", content = @Content(mediaType = "application/json"))
    public ResponseEntity<Response<Order>> deleteOrderByOrderId(@Parameter(description = "订单ID") @RequestParam("order_id") Integer order_id) {
        Response<Order> response = new Response<>();
        try {
            orderService.deleteOrderByOrderId(order_id);
            response.setCode(200);
            response.setMessage("订单取消成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.setCode(500);
            response.setMessage("服务器错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @Operation(summary = "订单发货")
    @PutMapping("/deliver")
    @ApiResponse(responseCode = "200", description = "订单发货成功", content = @Content(mediaType = "application/json"))
    public ResponseEntity<Response<Order>> deliver(@Parameter(description = "订单ID") @RequestParam("order_id") Integer order_id,
                                                   @Parameter(description = "订单状态") @RequestParam("order_state") String order_state) {
        Response<Order> response = new Response<>();
        try {
            Order order = orderService.findById(order_id);
            if (order == null) {
                response.setCode(500);
                response.setMessage("订单不存在");
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
            } else {
                Order neworder = orderService.deliver(order_id, order_state);
                response.setCode(200);
                response.setMessage("订单状态变更成功");
                response.setOrder(neworder);
                return ResponseEntity.ok(response);
            }
        } catch (Exception e) {
            response.setCode(500);
            response.setMessage("服务器错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }


}
